package com.creditcmsplt.util;

/**
 * Created by moon on 2017/10/21.
 * 授权书校验填写是否合法
 */
public class AuthorizationVerification {

    public int mFillInCount = 0;

    public AuthorizationVerification(){

    }

    /**
     * 判断授权书填写是否合法
     * @param content
     * @return
     */
    public boolean hasFillInIegally(String content){
        if (TextUtils.isEmpty(content)) return false;

        //逐步检查各行
        checkFirstLine(content);
        checkSecondLine(content);
        checkThirdLine(content);
        checkFourthLine(content);
        checkFifthLine(content);

//        System.out.println("AuthorizationVerification-----"+mFillInCount);
        if(mFillInCount < 1) return false;

        return true;
    }

    /**
     * 校验第一行
     * @return
     */
    private void checkFirstLine(String content){
        if (TextUtils.isEmpty(content)) return;

        //设置第一行中的关键字
        String [] keys = {"本人", "前身"};
        int keys1Index = content.indexOf(keys[0]);
        int keys2Index = content.indexOf(keys[1]);
        if(-1 == keys1Index || -1== keys2Index || keys1Index > keys2Index) {
            return;
        }

        String subContent = content.substring(keys1Index, keys2Index+2);
        if(TextUtils.isEmpty(subContent)){
            return;
        }
        subContent = subContent.replace("\r\n", "");
//        System.out.println("第一行内容：==========" + subContent);

        int key1Index = subContent.indexOf(keys[0]);
        if(key1Index == -1) return;
        if (subContent.length() > (key1Index + 5) &&
                queryContent(subContent.substring(key1Index, key1Index+5), "身份")){
            return;
        }

        int key3Index = subContent.indexOf("号码");
        if(key3Index == -1) return;
        if(subContent.length() > (key3Index+10) &&
                queryContent(subContent.substring(key3Index, key3Index+10), "联系电话")){
            return;
        }

        int key4Index = subContent.indexOf("联系电话");
        if(key4Index == -1) return;
        if (subContent.length() > (key4Index+8) &&
                queryContent(subContent.substring(key4Index, key4Index+8), "目前")){
            return;
        }

        if (subContent.length() < 19){
            return;
        }

        mFillInCount++; //第一行合法
//        System.out.println("true ========== ");
    }

    /**
     * 校验第二行
     * @param content
     */
    private void checkSecondLine(String content){
        if(TextUtils.isEmpty(content)) return;

        int firstKey = content.indexOf("本人");
        if(-1 == firstKey){
            return;
        }


        if(content.length() < (firstKey + 18)){
            return;
        }

        String subContent = content.substring(firstKey + 18);   //排除第一行中空格填入了第二行中的关键字的情况
        String[]keys = {"处", "县"};
        if(-1 == subContent.indexOf(keys[0]) || -1== subContent.indexOf(keys[1])) {
            return;
        }
        if(subContent.length() <= subContent.indexOf(keys[0]) + 3){
            return;
        }
        subContent = subContent.substring(subContent.indexOf(keys[0]), subContent.indexOf(keys[1])+3);
        if(TextUtils.isEmpty(subContent)){
            return;
        }

        subContent = subContent.replace("\r\n", "");
//        System.out.println("第2行内容：==========" + subContent);

        int key1Index = subContent.indexOf(keys[0]);
        if(key1Index == -1) return;
        if (subContent.length() > (key1Index + 2) &&
                queryContent(subContent.substring(key1Index, key1Index+2), "省")){
            return;
        }

        int key2Index = subContent.indexOf("省");
        if(key2Index == -1) return;
        if (subContent.length() > (key2Index+2) &&
                queryContent(subContent.substring(key2Index, key2Index+2), "市")){
            return;
        }

        int key3Index = subContent.indexOf("市");
        if(key3Index == -1) return;
        if (subContent.length() > (key3Index+3) &&
                queryContent(subContent.substring(key3Index, key3Index+3), "区")){
            return;
        }

        if(subContent.length() < 12){
            return;
        }

        mFillInCount++; //第二行合法
//        System.out.println("true ========== ");
    }

    /**
     * 校验第三行
     * @param content
     */
    private void checkThirdLine(String content){
        if(TextUtils.isEmpty(content)) return;

        int indexKey1 = content.indexOf("申请办理");
        int indexKey2 = content.indexOf("需要");

        if( indexKey1 == -1 || indexKey2 == -1) return;
        
        if(indexKey2<=indexKey1||indexKey2>content.length()){
            return;
        }

        String subContent = content.substring(indexKey1, indexKey2);
        if (TextUtils.isEmpty(subContent)) return;

        subContent = subContent.replace("\r\n", "");
//        System.out.println("申请办理 -------" + subContent + " -------业务");

        if (subContent.length() < 7) return;

        mFillInCount++; //第三行合法
//        System.out.println("true ========== ");
    }


    /**
     * 检测第四行（授权人签字按手印一行）
     * @param content
     */
    private void checkFourthLine(String content){
        if(TextUtils.isEmpty(content)) return;

        int indexKey1 = content.indexOf("签字按手印");
        int indexKey2 = content.indexOf("授权签字日期");

        if( -1 == indexKey1 || -1 == indexKey2) return;

        if(indexKey2<=indexKey1||indexKey2>content.length()){
            return;
        }
        
        String subContent = content.substring(indexKey1, indexKey2);
        if(TextUtils.isEmpty(subContent)){
            return;
        }
        subContent = subContent.replace("\r\n", "");
//        System.out.println("签名按手印：==========" + subContent);

        if (subContent.length() < 8) return;

        mFillInCount++; //签字合法
//        System.out.println("true ========== ");
    }

    /**
     * 校验签字日期
     * @param content
     */
    private void checkFifthLine(String content){
        if(TextUtils.isEmpty(content)) return;

        int indexKey = content.indexOf("授权签字日期");

        if( -1 == indexKey) return;

        String subContent = content.substring(indexKey);
        if(TextUtils.isEmpty(subContent)){
            return;
        }
        subContent = subContent.replace("\r\n", "");
//        System.out.println("日期：==========" + subContent);

        if (subContent.length() < 8) return;

        mFillInCount++; //签字合法
//        System.out.println("true ========== ");
    }

    /**
     * 查询源字符串中是否包含key
     * @param srcContent 源字符串
     * @param key 关键字
     * @return
     */
    private boolean queryContent(String srcContent, String key){
        if (TextUtils.isEmpty(srcContent) || TextUtils.isEmpty(key)) return false;

        if(srcContent.contains(key)) return true;

        return false;
    }
}
